from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .ser import *
from django.core.paginator import Paginator
import random
import jwt
from md import settings
from utils.comm import *
# 18595066676
import random
class Text(APIView):
    def post(self,request):
        conn=get_redis_conn()
        caa=str(conn.get(request.data.get('uuid')),encoding='utf8')
        zy=str(request.data.get('yz'))
        print(caa,zy)
        if caa.lower()==zy.lower():
            print(12321)
            mm=random.randint(100000,999999)
            mobile=request.data.get('mobile')
            res=send_message(mobile,mm,1)
            conn.setex('yzm',1000,mm)
            if res:
                return Response({'code':200})
            else:
                return Response({'code': 5000})
        else:
            return Response({'code':10010})



import re

class Regdd(APIView):
    def post(self,request):
        print(666)
        username=request.data.get('username')
        if not username:
            print(444)
            return Response({'code':10010})

        name = re.match('\w{5,10}$',username)
        if not name:
            return Response({'code': 10011})

        us=User.objects.filter(username=username).first()

        if us:
            return Response({'code': 10012})

        password1 = request.data.get('password')
        password2 = request.data.get('password2')

        if not password1:
            return Response({'code': 10016})

        pw1 = re.match('^\w{6,20}$', password1)
        if not pw1:
            return Response({'code': 10017})

        if not password2:
            return Response({'code': 10018})

        pw2 = re.match('^\w{6,20}$', password2)
        if not pw2:
            return Response({'code': 10019})

        if password1 != password2:
            print(1333)
            return Response({'code': 10020})
        mobile=request.data.get('mobile')
        if not mobile:
            return Response({'code': 10013})

        mob = re.match('^1[3-9]\d{9}$', mobile)
        if not mob:
            return Response({'code': 10014})

        # mo = User.objects.filter(mobile=mobile).first()
        #
        # if mo:
        #     return Response({'code': 10015})
        conn = get_redis_conn()
        print(conn.get('ymz'))
        return Response({'code':200})

class Useradd(APIView):
    def post(self,request):
        if not request.data.get('username'):
            return Response({'code':1021})
        if not request.data.get('password'):
            return Response({'code':1022})
        if not request.data.get('password2'):
            return Response({'code':1023})
        if not request.data.get('mobile'):
            return Response({'code':1024})
        username = request.data.get('username')
        us = User.objects.filter(username=username).first()
        if us:
            return Response({'code': 10012})
        # mobile = request.data.get('mobile')
        # mo = User.objects.filter(mobile=mobile).first()
        #
        # if mo:
        #     return Response({'code': 10015})
        conn = get_redis_conn()
        mob=str(conn.get('yzm'),encoding='utf8')
        if  mob!= request.data.get('dxyzm'):
            return Response({'code': 10026})

        ser=Userser(data=request.data)
        if ser.is_valid():
            ser.save()
            token = jwt.encode({'data': {'username': username}},
            settings.SECRET_KEY, algorithm='HS256')
            # us=User.objects.filter(username=username)
            return Response({'code': 200,'token':token})
        else:
            return Response({'code': 20020})


from django.http import HttpResponse
from utils.captcha.captcha import captcha
class Imgdd(APIView):
    def get(self,request):
        uuid = request.GET.get('uuid')
        print(uuid)
        # 生成验证码图片
        name, text, pic = captcha.generate_captcha()
        # 获取redis连接
        conn = get_redis_conn()
        # 图片验证码存储在redis里面，设置过期时间
        conn.set(uuid, text)  # 以秒为单位
        print(conn.get(uuid))
        # 以图片的方式输出
        print(text)
        return HttpResponse(pic, content_type='image/jpg')


class Logindd(APIView):
    def post(self,request):
        username=request.data.get('username')
        password=request.data.get('password')
        us=User.objects.filter(username=username).first()
        if us:
            if us.password==password:
                token = jwt.encode({'data': {'username': username}},
                                   settings.SECRET_KEY, algorithm='HS256')
                return Response({'code': 200, 'token': token,'userid':us.id})
        return Response({"code":10010})

class Addressdd(APIView):
    def get(self,request):
        paid=request.GET.get('id')
        if paid:
            us = Area.objects.filter(parent_id=paid)
        else:
            us=Area.objects.filter(parent_id__isnull=True)
        ser=Areaser(us,many=True).data
        return Response({'code':200,'list':ser})

    def post(self,request):
        userid=request.data.get("user")
        total = Address.objects.filter(user_id=userid).count()
        if total==ADDRESS_MAX_COUNT:
            return Response({"code": 30010, 'mes': '已经超过最大限制了'})
        ser=Addressser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code':200})
        else:
            return Response({'code': 10010})
    def put(self,request):
        id = request.data.get("id")
        us = Address.objects.filter(id=id).first()
        ser=Addressser(us,request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code":200})
        else:
            return Response({"code": 10010})

class addget(APIView):
    def get(self,request):
        uid=request.GET.get("uid")
        us=Address.objects.filter(user_id=uid)
        ser = sAddressser(us,many=True).data
        total = Address.objects.filter(user_id=uid).count()
        return Response({'code': 200, 'list': ser,'total':total,'zd':ADDRESS_MAX_COUNT})
    def put(self,request):
        id=request.GET.get('id')
        uid=request.GET.get('uid')
        us=Address.objects.filter(user_id=uid,is_default=1).update(is_default=0)
        us1=Address.objects.filter(id=id).update(is_default=1)
        return Response({"code":200})
    def post(self,request):
        id=request.data.get("id")
        us=Address.objects.filter(id=id).first()
        ser = sAddressser(us).data
        return Response({'code': 200, 'list': ser})
# Create your views here.
